<?php declare(strict_types=1);

/**
 * @package Snowflakes (Light Portal)
 * @link https://custom.simplemachines.org/index.php?mod=4244
 * @author Bugo <bugo@dragomano.ru>
 * @copyright 2023-2025 Bugo
 * @license https://opensource.org/licenses/MIT MIT
 *
 * @category plugin
 * @version 29.10.25
 */

namespace LightPortal\Plugins\Snowflakes;

use Bugo\Compat\Theme;
use LightPortal\Plugins\AssetBuilder;
use LightPortal\Plugins\Event;
use LightPortal\Plugins\Plugin;
use LightPortal\Plugins\PluginAttribute;
use LightPortal\Plugins\SettingsFactory;

if (! defined('LP_NAME'))
	die('No direct access...');

/**
 * Generated by PluginMaker
 */
#[PluginAttribute]
class Snowflakes extends Plugin
{
	private array $params = [
		'icon_color'       => '#a6e7ff',
		'icon_size'        => 15,
		'snowflakes_count' => 100,
		'snowdrifts_count' => 10,
	];

	public function init(): void
	{
		Theme::loadCSSFile('light_portal/snowflakes/snow.min.css');

		Theme::loadJavaScriptFile('light_portal/snowflakes/Snow.min.js', ['minimize' => true]);

		Theme::addInlineJavaScript('
			new Snow({
				iconColor: "' . (empty($this->context['icon_color']) ? $this->params['icon_color'] : $this->context['icon_color']) . '",
				iconSize: ' . (empty($this->context['icon_size']) ? $this->params['icon_size'] : $this->context['icon_size']) . ',
				countSnowflake: ' . (empty($this->context['snowflakes_count']) ? $this->params['snowflakes_count'] : $this->context['snowflakes_count']) . ',
				showSnowBalls: ' . (empty($this->context['enable_snowdrifts']) ? 'false' : 'true') . ',
				snowBallsLength: ' . (empty($this->context['snowdrifts_count']) ? $this->params['snowdrifts_count'] : $this->context['snowdrifts_count']) . '
			});', true);
	}

	public function addSettings(Event $e): void
	{
		$this->addDefaultValues($this->params);

		$e->args->settings[$this->name] = SettingsFactory::make()
			->color('icon_color')
			->int('icon_size', ['min' => 1])
			->range('snowflakes_count')
			->check('enable_snowdrifts')
			->range('snowdrifts_count', ['max' => 10])
			->toArray();
	}

	public function prepareAssets(Event $e): void
	{
		$builder = new AssetBuilder($this);
		$builder->scripts()->add('https://cdn.jsdelivr.net/gh/Alaev-Co/snowflakes/dist/Snow.min.js');
		$builder->css()->add('https://cdn.jsdelivr.net/gh/Alaev-Co/snowflakes/dist/snow.min.css');
		$builder->appendTo($e->args->assets);
	}

	public function credits(Event $e): void
	{
		$e->args->links[] = [
			'title' => 'Snowflakes',
			'link' => 'https://github.com/Alaev-Co/snowflakes',
			'author' => 'Alaev & Co',
			'license' => [
				'name' => 'The MIT License',
				'link' => 'https://github.com/Alaev-Co/snowflakes?tab=readme-ov-file#MIT-1-ov-file'
			]
		];
	}
}
